package client;

import event.Event;
import event.EventHandler;
import gui.ClientPanel;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

import javax.swing.JList;

public class ClientController extends ClientPanel {

	private final EventManager<Event, EventHandler> mEventManager = new EventManager<Event, EventHandler>();
	public final EventManager<Event, EventHandler> mPeerEventManager = new EventManager<Event, EventHandler>();

	private ComServer mComServer = null;

	private DirectoryListing mRemoteDirectory = new DirectoryListing();

	private DownloadManager mDownloadManager;

	private String mDirToShare;

	public ClientController(String dirToShare) {
		mDirToShare = dirToShare;

		mDownloadManager = new DownloadManager(this, mTableModel);

		registerEventHandlers();

		setDirectoryModels(mRemoteDirectory);

		updateRemoteAddress(mRemoteDirectory.getDirectory());
	}

	public void requestFile(String file) throws IOException {
		mComServer.requestFileHost(file);
	}

	/*
	 * Set methods.
	 */

	/*
	 * Get methods.
	 */

	public DirectoryListing getRemoteDirectory() {
		return mRemoteDirectory;
	}

	public DownloadManager getDownloadManager() {
		return mDownloadManager;
	}

	public void connect(InetAddress ip, int port) throws IOException {
		PeerServer server = new PeerServer(8181);

		log("> Attempting to connect...");

		mComServer = new ComServer(ip, port, mEventManager);

		Thread thread = new Thread(mComServer);
		thread.start();

		try {
			File file = new File(mDirToShare);

			mComServer.sendAvailableFileList(file);
		} catch (Exception e) {
			log("> You have chosen to share no files.");
		}

		log("> Successfully connected.");
	}

	/*
	 * Private methods.
	 */

	private void registerEventHandlers() {
		mEventManager.register(new ServerSearchResultEventHandler(this));
		mEventManager.register(new ServerHostNameEventHandler(this));

		new Thread(mEventManager).start();

		mPeerEventManager.register(new PeerFileResponseEventHandler(
				mDownloadManager));
		new Thread(mPeerEventManager).start();
	}

	public EventManager<Event, EventHandler> getPeerEventManager() {
		return mPeerEventManager;
	}

	@Override
	protected void search(String query) {
		try {
			mComServer.search(query);
		} catch (IOException e) {

		}
	}

	@Override
	protected void downloadClick() {
		JList list = getRemoteList();
		if (list.getSelectedIndex() >= 0) {
			int[] sel = list.getSelectedIndices();
			for (int i : sel) {
				try {
					requestFile((String) mRemoteDirectory.get(i));
				} catch (IOException e1) {

				}
			}

		}
	}

	@Override
	protected void resumeClick() {
		int[] rows = mDownloadTable.getSelectedRows();

		for (int i : rows) {
			String value = (String) mDownloadTable.getModel().getValueAt(i, 0);

			Download download = mDownloadManager.getDownload(value);
			if (download != null) {
				mDownloadManager.download(download);
			}
		}
	}

	@Override
	protected void pauseClick() {
		int[] rows = mDownloadTable.getSelectedRows();

		for (int i : rows) {
			String value = (String) mDownloadTable.getModel().getValueAt(i, 0);

			Download download = mDownloadManager.getDownload(value);
			if (download != null) {
				mDownloadManager.pauseDownload(download);
			}
		}
	}

	/*
	 * View event handlers
	 */

}
